﻿using System;
using System.Windows.Forms;
using TogNet.BusinessObjects;
using TogNet.Configuration;

namespace TogNet.Forms
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            Visible = false;
            ShowInTaskbar = false;

            base.OnLoad(e);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NetworkAdaptersSection.Settings.Save();
            Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {}

        private void AppTrayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    foreach (NetworkAdapter adapter in NetworkAdaptersSection.Settings.Adapters)
                    {
                        ConnectionToggler.ToggleState(adapter);

                        if (adapter.IsAlreadyEnabled && adapter.ShouldEnableProxy)
                        {
                            ProxyChanger.EnableProxy(true);
                        }
                        else if(adapter.IsAlreadyEnabled && !adapter.ShouldEnableProxy )
                        {
                            ProxyChanger.EnableProxy(false);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                AppTrayIcon.ShowBalloonTip(5000, "Error:", exception.Message, ToolTipIcon.Error);
            }
        }

        private void configToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (ConfigurationForm form = new ConfigurationForm())
            {
                form.DataSource = NetworkAdaptersSection.Settings.Adapters;
                form.ShowDialog();
            }
        }
    }
}
